# -*- coding: utf-8 -*-
"""
Created on Mon Oct  8 14:29:38 2018

@author: caixue1
"""

# 将从mongo数据库取出来的因子数据整合供后续决策树学习。整合的数据位于strategy_data文件夹，strategy_data_201001.csv表格里是201001的月度因子数据，其中关于return的因子数据是2月份的。

import pandas as pd
import copy
import os

path = 'D:/strategy_data'
filename = os.listdir(path)




monthlytime = ['2010-01-31 07:00:00+00:00', '2010-02-28 07:00:00+00:00', '2010-03-31 07:00:00+00:00', '2010-04-30 07:00:00+00:00', '2010-05-31 07:00:00+00:00', '2010-06-30 07:00:00+00:00', '2010-07-31 07:00:00+00:00', '2010-08-31 07:00:00+00:00', '2010-09-30 07:00:00+00:00', '2010-10-31 07:00:00+00:00', '2010-11-30 07:00:00+00:00', '2010-12-31 07:00:00+00:00',
               '2011-01-31 07:00:00+00:00', '2011-02-28 07:00:00+00:00', '2011-03-31 07:00:00+00:00', '2011-04-30 07:00:00+00:00', '2011-05-31 07:00:00+00:00', '2011-06-30 07:00:00+00:00', '2011-07-31 07:00:00+00:00', '2011-08-31 07:00:00+00:00', '2011-09-30 07:00:00+00:00', '2011-10-31 07:00:00+00:00', '2011-11-30 07:00:00+00:00', '2011-12-31 07:00:00+00:00',
               '2012-01-31 07:00:00+00:00', '2012-02-29 07:00:00+00:00', '2012-03-31 07:00:00+00:00', '2012-04-30 07:00:00+00:00', '2012-05-31 07:00:00+00:00', '2012-06-30 07:00:00+00:00', '2012-07-31 07:00:00+00:00', '2012-08-31 07:00:00+00:00', '2012-09-30 07:00:00+00:00', '2012-10-31 07:00:00+00:00', '2012-11-30 07:00:00+00:00', '2012-12-31 07:00:00+00:00',
               '2013-01-31 07:00:00+00:00', '2013-02-28 07:00:00+00:00', '2013-03-31 07:00:00+00:00', '2013-04-30 07:00:00+00:00', '2013-05-31 07:00:00+00:00', '2013-06-30 07:00:00+00:00', '2013-07-31 07:00:00+00:00', '2013-08-31 07:00:00+00:00', '2013-09-30 07:00:00+00:00', '2013-10-31 07:00:00+00:00', '2013-11-30 07:00:00+00:00', '2013-12-31 07:00:00+00:00',
               '2014-01-31 07:00:00+00:00', '2014-02-28 07:00:00+00:00', '2014-03-31 07:00:00+00:00', '2014-04-30 07:00:00+00:00', '2014-05-31 07:00:00+00:00', '2014-06-30 07:00:00+00:00', '2014-07-31 07:00:00+00:00', '2014-08-31 07:00:00+00:00', '2014-09-30 07:00:00+00:00', '2014-10-31 07:00:00+00:00', '2014-11-30 07:00:00+00:00', '2014-12-31 07:00:00+00:00',
               '2015-01-31 07:00:00+00:00', '2015-02-28 07:00:00+00:00', '2015-03-31 07:00:00+00:00', '2015-04-30 07:00:00+00:00', '2015-05-31 07:00:00+00:00', '2015-06-30 07:00:00+00:00', '2015-07-31 07:00:00+00:00', '2015-08-31 07:00:00+00:00', '2015-09-30 07:00:00+00:00', '2015-10-31 07:00:00+00:00', '2015-11-30 07:00:00+00:00', '2015-12-31 07:00:00+00:00',
               '2016-01-31 07:00:00+00:00', '2016-02-29 07:00:00+00:00', '2016-03-31 07:00:00+00:00', '2016-04-30 07:00:00+00:00', '2016-05-31 07:00:00+00:00', '2016-06-30 07:00:00+00:00', '2016-07-31 07:00:00+00:00', '2016-08-31 07:00:00+00:00', '2016-09-30 07:00:00+00:00', '2016-10-31 07:00:00+00:00', '2016-11-30 07:00:00+00:00', '2016-12-31 07:00:00+00:00',
               '2017-01-31 07:00:00+00:00', '2017-02-28 07:00:00+00:00', '2017-03-31 07:00:00+00:00', '2017-04-30 07:00:00+00:00', '2017-05-31 07:00:00+00:00', '2017-06-30 07:00:00+00:00', '2017-07-31 07:00:00+00:00', '2017-08-31 07:00:00+00:00', '2017-09-30 07:00:00+00:00', '2017-10-31 07:00:00+00:00', '2017-11-30 07:00:00+00:00', '2017-12-31 07:00:00+00:00',
               '2018-01-31 07:00:00+00:00', '2018-02-28 07:00:00+00:00', '2018-03-31 07:00:00+00:00', '2018-04-30 07:00:00+00:00', '2018-05-31 07:00:00+00:00', '2018-06-30 07:00:00+00:00', '2018-07-31 07:00:00+00:00', '2018-08-31 07:00:00+00:00', '2018-09-30 07:00:00+00:00']
path = 'D:/new_factor'
filename = os.listdir(path)
for i in range(len(monthlytime) - 1):
    print (i)
#for i in range(1):
    time = monthlytime[i]
    year = time[:4]
    month = time[5:7]
    nexttime = monthlytime[i+1]

    data_table = pd.DataFrame()
    for file in filename:
            test_factor = pd.read_csv('D:/new_factor/' + file, encoding = 'GBK').set_index('Symbol')
            if 'Return' not in file:
                data = test_factor[[time]]
            elif 'Return' in file:
                data = test_factor[[nexttime]]
            data.columns = [file[7:-4]]
            data_table = pd.concat([data_table, data], axis = 1)
    train_data = pd.read_csv('D:/test/train_data_' + year + month + '.csv', encoding = 'GBK').set_index('Unnamed: 0')
    strategy_data = pd.concat([train_data, data_table], axis = 1)
    strategy_data.to_csv('D:/strategy_data/strategy_data_' + year + month + '.csv', encoding = 'GBK')



